home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / Movie3.0 / Source / Animation.m < prev    next >
Encoding:
Text File  |  1995-07-18  |  8.2 KB  |  288 lines

  1.  
  2. /*
  3.  *    Adaption of Mark Podlipec's xanim software to Movie.app 
  4.  *    (c) 1995 Andreas Windemuth
  5.  */
  6.  
  7. #define VERBOSE 0
  8.  
  9. /*
  10.  * xanim.c
  11.  *
  12.  * Copyright (C) 1990,1991,1992,1993,1994 by Mark Podlipec. 
  13.  * All rights reserved.
  14.  *
  15.  * This software may be freely copied, modified and redistributed without
  16.  * fee for non-commerical purposes provided that this copyright notice is
  17.  * preserved intact on all copies and modified copies.
  18.  * 
  19.  * There is no warranty or other guarantee of fitness of this software.
  20.  * It is provided solely "as is". The author(s) disclaim(s) all
  21.  * responsibility and liability with respect to this software's usage
  22.  * or its effect upon hardware or computer systems.
  23.  *
  24.  */
  25. #define DA_REV 2.68
  26. #define DA_MINOR_REV 5
  27.  
  28. #define SCALING 0
  29.  
  30. #include <libc.h>
  31.  
  32. #import "Animation.h"
  33.  
  34. @implementation Animation
  35.  
  36. - initFrom:(const char *)filename
  37. {
  38.     BOOL result;
  39.  
  40.     xa_init();
  41.     xa_verbose = FALSE;
  42.     xa_debug = 0;
  43.     xa_file_flag = TRUE;
  44.     first_file = 0;
  45.     cur_file = Get_Anim_Hdr(cur_file, filename);
  46.     cur_file->anim_type = Determine_Anim_Type(filename);
  47.     cur_file->anim_flags = 0;
  48.     switch(cur_file->anim_type)
  49.     {
  50.         case IFF_ANIM:
  51.       if (xa_verbose) fprintf(stderr,"Reading IFF File %s\n",filename);
  52.       result = IFF_Read_File(filename,cur_file);
  53.       break;
  54.         case GIF_ANIM:
  55.       if (xa_verbose) fprintf(stderr,"Reading GIF File %s\n",filename);
  56.       result = GIF_Read_Anim(filename,cur_file);
  57.       break;
  58.         case TXT_ANIM:
  59.       if (xa_verbose) fprintf(stderr,"Reading TXT File %s\n",filename);
  60.       result = TXT_Read_File(filename,cur_file);
  61.       break;
  62.         case FLI_ANIM:
  63.       if (xa_verbose) fprintf(stderr,"Reading FLI File %s\n",filename);
  64.       result = Fli_Read_File(filename,cur_file);
  65.       break;
  66.         case DL_ANIM:
  67.       if (xa_verbose) fprintf(stderr,"Reading DL File %s\n",filename);
  68.       result = DL_Read_File(filename,cur_file);
  69.       break;
  70.         case PFX_ANIM:
  71.       if (xa_verbose) fprintf(stderr,"Reading PFX File %s\n",filename);
  72.       result = PFX_Read_File(filename,cur_file);
  73.       break;
  74. #if SETFILE
  75.         case SET_ANIM:
  76.       if (xa_verbose) fprintf(stderr,"Reading SET File %s\n",filename);
  77.       result = SET_Read_File(filename,cur_file);
  78.       break;
  79. #endif
  80.         case RLE_ANIM:
  81.       if (xa_verbose) fprintf(stderr,"Reading RLE File %s\n",filename);
  82.       result = RLE_Read_File(filename,cur_file);
  83.       break;
  84.         case AVI_ANIM:
  85.       if (xa_verbose) fprintf(stderr,"Reading AVI File %s\n",filename);
  86.       result = AVI_Read_File(filename,cur_file);
  87.       break;
  88.         case QT_ANIM:
  89.       if (xa_verbose) fprintf(stderr,"Reading QT File %s\n",filename);
  90.       result = QT_Read_File(filename,cur_file);
  91.       break;
  92.         case NOFILE_ANIM:
  93.       fprintf(stderr,"File %s not found\n",filename);
  94.       result = FALSE;
  95.       break;
  96.         default:
  97.       fprintf(stderr,"Unknown or unsupported animation type: %s\n",
  98.                                 filename);
  99.       result = FALSE;
  100.       break;
  101.       }
  102.             if (result == FALSE) cur_file = Return_Anim_Hdr(cur_file);
  103.       else
  104.       {
  105. #if SCALING
  106.     ULONG tmpx,tmpy;
  107. #endif
  108.         /* Setup up anim header.  */
  109.         cur_file->loop_num = DEFAULT_LOOPEACH_FLAG;
  110.  
  111.     cur_file->pause_lst = 0;
  112.  
  113.     xa_imagex = cur_file->imagex;
  114.     xa_imagey = cur_file->imagey;
  115.     if (xa_imagex > xa_max_imagex) xa_max_imagex = xa_imagex;
  116.     if (xa_imagey > xa_max_imagey) xa_max_imagey = xa_imagey;
  117.  
  118. #if SCALING
  119.     /* Handle Buffer Scaling Here */
  120.     if ( (xa_buff_x != 0) && (xa_buff_y != 0) )
  121.         {tmpx = xa_buff_x; tmpy = xa_buff_y;}
  122.     else if (xa_buff_x != 0) /* if just x, then adjust y */
  123.         {tmpx = xa_buff_x; tmpy = (xa_imagey * xa_buff_x) / xa_imagex;}
  124.     else if (xa_buff_y != 0) /* if just y, then adjust x */
  125.         {tmpy = xa_buff_y; tmpx = (xa_imagex * xa_buff_y) / xa_imagey;}
  126.     else
  127.     {
  128.         /* handle any scaling */
  129.       tmpx = (ULONG)((float)(xa_imagex) * xa_bscalex);
  130.       if (tmpx == 0) tmpx = xa_imagex;
  131.       tmpy = (ULONG)((float)(xa_imagey) * xa_bscaley);
  132.       if (tmpy == 0) tmpy = xa_imagey;
  133.     }
  134.     cur_file->buffx = tmpx;
  135.     cur_file->buffy = tmpy;
  136.  
  137.         /* Handle Display Scaling Here */
  138.     if ( (xa_disp_x != 0) && (xa_disp_y != 0) ) 
  139.         {tmpx = xa_disp_x; tmpy = xa_disp_y;}
  140.     else if (xa_disp_x != 0) /* if just x, then adjust y */
  141.         {tmpx = xa_disp_x; tmpy = (xa_imagey * xa_disp_x) / xa_imagex;}
  142.     else if (xa_disp_y != 0) /* if just y, then adjust x */
  143.         {tmpy = xa_disp_y; tmpx = (xa_imagex * xa_disp_y) / xa_imagey;}
  144.     else
  145.     {
  146.         /* handle any scaling */
  147.       tmpx = (ULONG)((float)(xa_imagex) * xa_scalex);
  148.       if (tmpx == 0) tmpx = xa_imagex;
  149.       tmpy = (ULONG)((float)(xa_imagey) * xa_scaley);
  150.       if (tmpy == 0) tmpy = xa_imagey;
  151.     }
  152.     /* handle any IFF laced images */
  153.     if ( (xa_allow_lace==TRUE) && (cur_file->anim_flags & ANIM_LACE))
  154.         tmpy >>= 1;
  155.     else    cur_file->anim_flags &= ~ANIM_LACE;
  156.     cur_file->dispx = tmpx;
  157.     cur_file->dispy = tmpy;
  158.     if (tmpx > xa_max_disp_x) xa_max_disp_x = tmpx;
  159.     if (tmpy > xa_max_disp_y) xa_max_disp_y = tmpy;
  160.  
  161.     if ((cmap_dither_type == CMAP_DITHER_FLOYD) && (xa_buffer_flag==FALSE))
  162.             cur_file->anim_flags |= ANIM_3RD_BUF;
  163.     xa_merged_anim_flags |= cur_file->anim_flags;
  164.  
  165.     /* NOTE: removed fade, remember to readd eventually */
  166.  
  167.     if (xa_time_flag == TRUE)
  168.     {
  169.       LONG time_int;
  170.       xa_time_end = XA_Time_Read();
  171.       time_int = xa_time_end - xa_time_start;
  172.       fprintf(stderr,"time = %ld\n",time_int);
  173.       xa_time_start = XA_Time_Read();
  174.     }
  175. #endif
  176.       } /* valid animation file */
  177.  
  178.     if (first_file == 0) return nil;
  179.  
  180.     if (first_file->anim_flags&ANIM_USE_FILE) xa_anim_flags |= ANIM_USE_FILE;
  181.     if (xa_anim_flags & ANIM_USE_FILE)
  182.     {
  183.       if (xa_fd>=0) { close(xa_fd); xa_fd = -1; }
  184.       if (xa_codec_buf) { FREE(xa_codec_buf,0x99); xa_codec_buf=0;}
  185.       if ( (xa_fd=open(cur_file->fname,O_RDONLY,NULL)) == 0)
  186.       { 
  187.         fprintf(stderr,"can't open file %s for reading\n",cur_file->fname); 
  188.         TheEnd();
  189.       }
  190.       xa_codec_buf = (char *)malloc( cur_file->max_fsize );
  191.       if (xa_codec_buf==0) TheEnd1("malloc codec_buf err");
  192.       if (VERBOSE) debug("File %s opened on %d, %d bytes allocated\n",
  193.         cur_file->fname, xa_fd, cur_file->max_fsize);
  194.     }
  195.  
  196.     cur_file = first_file;
  197.     bitmap = [[NXBitmapImageRep alloc] initData:NULL
  198.     pixelsWide:xa_max_imagex
  199.     pixelsHigh:xa_max_imagey
  200.     bitsPerSample:8
  201.     samplesPerPixel:3    // (cSpace == RGB_COLOR) ? 3 : 1
  202.     hasAlpha:NO
  203.     isPlanar:NO
  204.     colorSpace:NX_RGBColorSpace
  205.     bytesPerRow:0
  206.     bitsPerPixel:32
  207.     ];
  208.     if (!bitmap) {
  209.     warning("Could not create bitmap\n");
  210.     return nil;
  211.     }
  212.     if (VERBOSE) debug("Created Image %d %d %d\n", xa_max_imagex, xa_max_imagey, 24);
  213.     return self;
  214. }
  215.  
  216. - free;
  217. {
  218.     [bitmap free];
  219.     return self;
  220. }
  221.  
  222. - (NXBitmapImageRep *)update
  223. {
  224.     XA_ACTION *act;
  225.     ACT_DLTA_HDR *d;
  226.     ULONG xs, ys, xe, ye;
  227.     
  228.     if ( (act = cur_file->frame_lst[cur_frame].act) != 0) {
  229.     if (VERBOSE) debug("Action %d, type %ld chdr %x map %x\n", cur_frame, 
  230.         act->type, act->chdr, act->chdr->map);
  231.     if (act->type!=ACT_DELTA) {
  232.         warning("Action type %d not supported\n", act->type);
  233.         return nil;
  234.     }
  235.     } else {
  236.     warning("No Action in frame\n");
  237.     return nil;
  238.     }
  239.     d = (ACT_DLTA_HDR *)act->data;
  240.     if (!d) {
  241.     warning("No Delta in action\n");
  242.     return nil;
  243.     }
  244.     if (VERBOSE) debug(
  245.     "Delta %ld %ld %ld %ld flags=%lx special=%lx extra=%lx fpos=%ld fsize=%ld\n",
  246.     d->xpos, d->ypos, d->xsize, d->ysize, d->flags, d->special, d->extra, d->fpos, d->fsize
  247.     );
  248.     if ((xa_fd >= 0) && (!(d->flags & DLTA_DATA)) ) {
  249.     XA_Read_Delta(xa_codec_buf, xa_fd, d->fpos, d->fsize);
  250.     if (VERBOSE) debug("% bytes read at %d from %d\n", d->fsize, d->fpos, xa_fd);
  251.     d->delta([bitmap data], xa_codec_buf, d->fsize, act->chdr, act->chdr->map, 1,
  252.         d->xsize,d->ysize,24, &xs,&ys,&xe,&ye,d->special,d->extra);
  253.     d->delta([bitmap data], xa_codec_buf, d->fsize,
  254.         act->chdr, act->chdr->map, 1, d->xsize,d->ysize,24,
  255.         &xs,&ys,&xe,&ye,d->special,d->extra);
  256.     } else {
  257.     d->delta([bitmap data], d->data, d->fsize, act->chdr, act->chdr->map, 1,
  258.         d->xsize,d->ysize,24, &xs,&ys,&xe,&ye,d->special,d->extra);
  259.     }
  260.     if (VERBOSE) debug("Delta area: %ld %ld %ld %ld\n", xs, ys, xe, ye);
  261.     return bitmap;
  262. }
  263.  
  264. - getMaxSize:(NXSize *)size
  265. {
  266.     size->width = xa_max_imagex;
  267.     size->height = xa_max_imagey;
  268.     return self;
  269. }
  270.  
  271. - (NXBitmapImageRep *)next
  272. {
  273.     Step_Frame_Next();
  274.     return [self update];
  275. }
  276.  
  277. - (BOOL)isLast
  278. {
  279.     return (cur_frame==cur_file->last_frame);
  280. }
  281.  
  282. - (NXBitmapImageRep *)bitmap;
  283. {
  284.     return bitmap;
  285. }
  286.  
  287. @end
  288.